package com.weixin.base.web;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.omg.CORBA.Environment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.weixin.base.dao.impl.UserDaoImpl;
import com.weixin.base.service.UserService;
import com.weixin.base.util.HanzizhuanPin;
import com.weixin.base.util.Tools;
import com.weixin.entity.BgUser;

@Controller
public class Example {
	
	// 与接口配置信息中的Token要一致
	private static String token = "ceshitoken";

	/**
	 * 确认请求来自微信服务器
	 */
	@RequestMapping(value = "/ceshi2.do", method = RequestMethod.GET)
	public void ceshiGet(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		System.out.println("GET");
		String userAgent = request.getHeader("user-agent");
		System.out.println(userAgent);
		
		// 微信加密签名
		String signature = Tools.convertRs(request.getParameter("signature"));
		// 时间戳
		String timestamp = Tools.convertRs(request.getParameter("timestamp"));
		// 随机数
		String nonce = Tools.convertRs(request.getParameter("nonce"));
		// 随机字符串
		String echostr = Tools.convertRs(request.getParameter("echostr"));

		PrintWriter out = response.getWriter();
		// 通过检验signature对请求进行校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败
		if (Tools.checkSignature(signature, timestamp, nonce,"")) {
			out.print(echostr);
		}
		out.close();
		out = null;

		// 返回指令
		// response.setCharacterEncoding("UTF-8");
		// response.setContentType("text/plain");
		// response.getWriter().print(userAgent);
	}

	@RequestMapping(value = "/ceshi2.do", method = RequestMethod.POST)
	public void ceshiPost(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		System.out.println("POST");
		String userAgent = request.getHeader("user-agent");
		System.out.println(userAgent);
		// 返回指令
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/plain");
		response.getWriter().print(userAgent);
	}
	
	@RequestMapping(value = "/ceshickeditor.do", method = RequestMethod.POST)
	public void ceshickeditor(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		System.out.println("POST");
		String detail = request.getParameter("detail");
		System.out.println(detail);
		request.setAttribute("detail", detail);
//		req.getRequestDispatcher("../detail.jsp").forward(req, resp);
	}
	
	@RequestMapping(value = "/ceshickeditor.do", method = RequestMethod.GET)
	public void ceshickeditorget(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		System.out.println("GET");
		String detail = request.getParameter("detail");
		System.out.println(detail);
		request.setAttribute("detail", detail);
//		req.getRequestDispatcher("../detail.jsp").forward(req, resp);
	}

}